package org.example.handler;

import lombok.extern.slf4j.Slf4j;
import org.apache.tomcat.util.http.fileupload.impl.FileSizeLimitExceededException;
import org.apache.tomcat.util.http.fileupload.impl.SizeException;
import org.example.dao.commom.Result;
import org.example.dao.commom.ResultCode;
import org.example.exception.*;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.web.HttpMediaTypeNotAcceptableException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MaxUploadSizeExceededException;

@ControllerAdvice
@Slf4j
public class BaseExceptionHandler {


    //error
    @ExceptionHandler(value = HttpMediaTypeNotAcceptableException.class)
    @ResponseBody
    public Result HttpMediaTypeNotAcceptableException(HttpMediaTypeNotAcceptableException e) {
        log.error("非法的操作！");
        return new Result(ResultCode.FUCK_ERROR,"fuck you!");
    }

    @ExceptionHandler(value = ServerErrorException.class)
    @ResponseBody
    public Result ServerErrorException(ServerErrorException e) {
        log.error(e.getMsg());
        return new Result(e.getCode(),e.getMsg());
    }

    @ExceptionHandler(value = JsonException.class)
    @ResponseBody
    public Result JsonException(JsonException e) {
        log.error(e.getMsg());
        return new Result(e.getCode(),e.getMsg());
    }

    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public Result errorRuntimeException(Exception e) {
        log.error(e.getMessage());
        return new Result(ResultCode.FAIL,null);
    }


    //warn
    @ExceptionHandler(value = ParamLossErrorException.class)
    @ResponseBody
    public Result ParamLossErrorException(ParamLossErrorException e) {
        log.warn(e.getMsg());
        return new Result(e.getCode(),e.getMsg());
    }

    @ExceptionHandler(value = AuthException.class)
    @ResponseBody
    public Result AuthException(AuthException e) {
        log.warn(e.getMsg());
        return new Result(e.getCode(),e.getMsg());
    }

    @ExceptionHandler(value = PassMatchErrorException.class)
    @ResponseBody
    public Result PassMatchErrorException(PassMatchErrorException e) {
        log.warn(e.getMsg());
        return new Result(e.getCode(),e.getMsg());
    }

    @ExceptionHandler(value = EsException.class)
    @ResponseBody
    public Result EsException(EsException e) {
        log.warn(e.getMsg());
        return new Result(e.getCode(),e.getMsg());
    }



    //info
    @ExceptionHandler(value = HttpMessageNotReadableException.class)
    @ResponseBody
    public Result HttpMessageNotReadableException(HttpMessageNotReadableException e) {
        return new Result(ResultCode.PARAM_LOSS_ERROE,"请传递正常参数");
    }

    @ExceptionHandler(value = MaxUploadSizeExceededException.class)
    @ResponseBody
    public Result SizeException(MaxUploadSizeExceededException e) {
        return new Result(ResultCode.IMAGE_ERROR,"请上传正常一点的头像");
    }

    @ExceptionHandler(value = ImageEroorException.class)
    @ResponseBody
    public Result ImageEroorException(ImageEroorException e) {
        log.info(e.getMsg());
        return new Result(e.getCode(),e.getMsg());
    }

    @ExceptionHandler(value = PassErrorException.class)
    @ResponseBody
    public Result PassErrorException(PassErrorException e) {
        log.info(e.getMsg());
        return new Result(e.getCode(),e.getMsg());
    }
    @ExceptionHandler(value = CookieLoseException.class)
    @ResponseBody
    public Result CookieLoseException(CookieLoseException e) {
        log.info(e.getMsg());
        return new Result(e.getCode(),e.getMsg());
    }



















    @ExceptionHandler(value = JwErrorException.class)
    @ResponseBody
    public Result JwErrorException(JwErrorException e) {
        log.info(e.getMsg());
        return new Result(e.getCode(),e.getMsg());
    }















}
